package com.tiantianyuegou.app;

import java.io.File;

import android.content.Intent;
import android.graphics.Bitmap;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.tiantianyuegou.http.SPHelper;
import android.app.Application;
import android.content.Context;
import android.graphics.BitmapFactory.Options;
import android.location.LocationManager;
import android.provider.Settings;
import android.widget.Toast;

//import net.tsz.afinal.FinalBitmap;

/**
 * ImageLolder的初始化
 */
public class YiLaGuanApp extends Application{
	public static YiLaGuanApp app;
	//public static FinalBitmap sFinalBitmap;

	public static YiLaGuanApp getInstance() {

		return app;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		SPHelper.getInstance(this);
		app = this;
		initImageLoader();
	}

	private void initImageLoader() {
//		sFinalBitmap = FinalBitmap.create(this).configBitmapLoadThreadSize(10)
//				.configLoadingImage(R.mipmap.ic_launcher);
		initImageLoader(getApplicationContext());
		initImgLoader();
	}

	private void initImgLoader() {
		File cacheDir = StorageUtils.getOwnCacheDirectory(this,
				"imageloader/Cache");
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				this)
				.memoryCacheExtraOptions(480, 800)
				// max width, max height，即保存的每个缓存文件的最大长宽
				// .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75,
				// null) // Can slow ImageLoader, use it carefully (Better don't
				// use it)/设置缓存的详细信息，最好不要设置这个
				.threadPoolSize(3)
				// 线程池内加载的数量
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
				// You can pass your own memory cache
				// implementation/你可以通过自己的内存缓存实现
				.memoryCacheSize(2 * 1024 * 1024)
				.diskCacheSize(50 * 1024 * 1024)
				.diskCacheFileNameGenerator(new Md5FileNameGenerator())
				// 将保存的时候的URI名称用MD5 加密
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.diskCacheFileCount(100)
				// 缓存的文件数量
				//.diskCache(new UnlimitedDiscCache(cacheDir))
				// 自定义缓存路径
				//.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
				.imageDownloader(
						new BaseImageDownloader(this, 5 * 1000, 30 * 1000)) // connectTimeout
				// (5
				// s),
				// readTimeout
				// (30
				// s)超时时间
				// .writeDebugLogs() // Remove for release app
				.build();// 开始构建
		// Initialize ImageLoader with configuration.

		ImageLoader.getInstance().init(config);
	}

	public void initImageLoader(Context context) {
		Options options = new Options();
		options.inSampleSize = 2;
		DisplayImageOptions defaultDisplayImageOptions = new DisplayImageOptions.Builder()
				.bitmapConfig(Bitmap.Config.ARGB_4444).cacheOnDisk(true)
				.decodingOptions(options).cacheInMemory(false).build();
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context).threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.memoryCacheSizePercentage(5)
				.diskCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.threadPoolSize(30)
				.defaultDisplayImageOptions(defaultDisplayImageOptions).build();
		ImageLoader.getInstance().init(config);
	}
}
